package com.wcl.demo.utils;

import android.content.Context;
import android.content.DialogInterface;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.wcl.demo.model.entity.SearchHimUser;
import com.wcl.demo.ui.adapter.ChatAdapter;
import com.wcl.demo.view.CustomDialog;
import com.wcl.demo.view.CustomToast;

import java.util.List;

/**
 * Created by dllo on 15/12/1.
 */
public class DialogUtils {


    public static void dialogShow(Context context, String message, String title) {
        CustomDialog.Builder builder = new CustomDialog.Builder(context);
        builder.setMessage(message);
        builder.setTitle(title);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    public static void deleteSigleChat(final Context context, final String username, final String messageId, final ChatAdapter adapter) {
        CustomDialog.Builder builder = new CustomDialog.Builder(context);
        builder.setMessage("是否删除该条消息?");
        builder.setTitle("提示信息");
        final SearchHimUser finalUser = null;
        final String finalHeadimageurl = null;
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                ProgressUtils.showProgress(context);
                //删除当前会话的某条聊天记录
                EMConversation conversation = EMChatManager.getInstance().getConversation(username);
                conversation.removeMessage(messageId);
                List<EMMessage> newMsg = conversation.getAllMessages();
                adapter.addData(newMsg, finalHeadimageurl);
                ProgressUtils.dismissProgress();
                CustomToast.toastShow(context, "该条消息删除成功!");
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    public static void deleteChat(final Context context, final String groupid) {
        CustomDialog.Builder builder = new CustomDialog.Builder(context);
        builder.setMessage("是否清空聊天记录?");
        builder.setTitle("提示信息");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                ProgressUtils.showProgress(context);
                //删除当前会话的某条聊天记录

                //清空和某个user的聊天记录(包括本地)，不删除conversation这个会话对象
                EMChatManager.getInstance().clearConversation(groupid);
                ProgressUtils.dismissProgress();
                CustomToast.toastShow(context, "该条消息删除成功!");
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
}
